package com.rtx.edi.platform.express.auth.handler;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.edi.model.express.auth.po.PlAuthApp;
import com.rtx.edi.model.express.auth.po.PlAuthShopApp;
import com.rtx.edi.model.express.auth.po.PlExpressTemplate;
import com.rtx.edi.platform.express.auth.service.ExpTemplatesService;
import com.rtx.edi.platform.express.auth.service.PlAuthAppService;
import com.rtx.edi.platform.express.auth.service.PlAuthShopAppService;
import com.rtx.edi.platform.express.auth.service.PlExpressTemplateService;
import com.taobao.api.response.CainiaoCloudprintStdtemplatesGetResponse;
import com.taobao.api.response.CainiaoCloudprintStdtemplatesGetResponse.StandardTemplateDo;
import com.taobao.api.response.CainiaoCloudprintStdtemplatesGetResponse.StandardTemplateResult;

@Component
public class ExpressTemplateHandler {
	
	@Autowired
	private PlAuthShopAppService shopappService;
	
	@Autowired
	private PlAuthAppService plAuthAppService;
	
	@Autowired
	private ExpTemplatesService expTemplatesService;
	
	@Autowired
	private PlExpressTemplateService plExpressTemplateService;
	
	public void handler() throws Exception{
		 LambdaQueryWrapper<PlAuthShopApp> query= Wrappers.lambdaQuery();
		 query.eq(PlAuthShopApp::getActive, "1")
		 .eq(PlAuthShopApp::getDeleteflag, "0");
		 List<PlAuthShopApp> reqList= shopappService.getBaseMapper().selectList(query);
		 for (PlAuthShopApp vo : reqList) {
			 PlAuthApp app= plAuthAppService.getById(vo.getAppid());
			 String type=app.getType(); // 应用所属平台
			 switch (type) {
			 	case "TB":
			 		CainiaoCloudprintStdtemplatesGetResponse resp=expTemplatesService.getTbTemplate(app.getGetway(), app.getAppkey(), app.getAppsecret());
			 		analysisTbresp(resp);
			 		break;
				case "DY":

					break;
				case "PDD":
					
					break;
				case "JD":
					
					break;
				case "XHS":
					
					break;
				case "DW":
					
					break;
				default:
					
					break;
				}
			 
		 }
	
	
	}
	
	/**
	 * 解析淘宝返回报文
	 * @param resp
	 */
	private void analysisTbresp(CainiaoCloudprintStdtemplatesGetResponse resp) {
		if(resp.getResult()!=null) {
			for (StandardTemplateResult temp : resp.getResult().getDatas()) {
				for (StandardTemplateDo tempdo : temp.getStandardTemplates()) {
					PlExpressTemplate po=new PlExpressTemplate();
					po.setSalesp("TB"); // 平台
					po.setCpcode(temp.getCpCode()); // 物流服务商id
					po.setStandardtemplateid(tempdo.getStandardTemplateId());
					po.setStandardtemplatename(tempdo.getStandardTemplateName());
					po.setStandardtemplateurl(tempdo.getStandardTemplateUrl());
					po.setStandardwaybilltype(String.valueOf(tempdo.getStandardWaybillType()));
//					po.setBrandcode(tempdo.getBrandCode());
					po.setAddwho("ris_edi_dev");
					po.setEditwho("ris_edi_dev");
					// 不存在新增 存在更新 
					QueryWrapper<PlExpressTemplate> qw =new QueryWrapper<PlExpressTemplate>();
					qw.eq("salesp", "TB")
					.eq("cpcode", temp.getCpCode())
					.eq("standardtemplateid", tempdo.getStandardTemplateId())
					;
					boolean isexist=plExpressTemplateService.getBaseMapper().exists(qw);
					if(isexist) {
						PlExpressTemplate old=plExpressTemplateService.getOne(qw);
						po.setId(old.getId());
						BeanUtils.copyProperties(po,old);
						plExpressTemplateService.updateById(old);
						
					}else {
						plExpressTemplateService.addPlExpressTemplate(po);
						
					}
					
				}
			}
			
			
		}

		
	}

}
